<!DOCTYPE html>
<html>
<head>
	<title>LokiJS benchmarks</title>
	<script src="taffy.js"></script>
	<script src="nedb.js"></script>
	<script src="../src/lokijs.js"></script>
</head>
<body>
<button id="runTaffy">run Taffy</button>
<button id="runLoki">run Loki</button>
<button id="runNeDB">run NeDB</button>
<script type="text/javascript">

document.getElementById('runLoki').addEventListener('click', runLokiBenchmark);
document.getElementById('runTaffy').addEventListener('click', runTaffyBenchmark);
document.getElementById('runNeDB').addEventListener('click', runNeDBBenchmark);


var size = 1000;

var generateData = function(size){

	function getRandomInt (min, max) {
    	return Math.floor(Math.random() * (max - min + 1)) + min;
	}

	var coll = [];

	var az = 'qwertyuiopasdfghjklzxcvbnm'.split('');
	var azLength = az.length >>> 0;

	var len = size || 100;
	while(len--){
		var random = getRandomInt(0, azLength);
		coll.push( {name : az[random], age : random});
	}

	return coll;
};

var coll = generateData(size);

function runNeDBBenchmark(){
	
	var db = new Nedb();
	var i = size;
	console.profile('Profiling NeDB');
	while(i--){
		db.insert(coll[i]);
	}
	console.trace();
	console.profileEnd();
}

function runTaffyBenchmark(){
	
	console.log('Init...');


	// initialize taffy
	var friends = TAFFY();
	
	var i = size;
	console.profile('Profiling Taffy');
	while(i--){
		friends(coll[i]);
	}
	console.trace();
	console.profileEnd();

}

function runLokiBenchmark(){
	// initialize loki
	var db = new loki('db');
	var lokis = db.addCollection('friends','Friend');

	var j = size;
	console.profile('Profiling Loki');
	while(j--){
		lokis.document(coll[j]);
	}
	console.trace();
	console.profileEnd();
}



</script>

</body>
</html>